package org.quasar.airNova.presentationLayer;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;

public class SendMailTLS {

	private static String username = "mhdoa@iscte-iul.pt";
	private static String password = "Druidas112";

	public SendMailTLS(String username, String password) {
		this.setUsername(username);
		this.setPassword(password);
	}

	public static void sendEmail(String destinatary, String content) {

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(getUsername(),
								getPassword());
					}
				});

		try {

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("mhdoa@iscte-iul.pt"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(destinatary));
			message.setSubject("Testing Subject");
			message.setText(content);

			Transport.send(message);

			System.out.println("Done");

		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}

	public static String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public static String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public static void main(String[] args) {
		SendMailTLS.sendEmail("mhdoa@iscte-iul.pt", "Teste");
	}

	public static void askForEmail(String text) {
		if(JOptionPane.showConfirmDialog(null, "Deseja Enviar por email?") == 0) 
			new EmailFrame(text);	
		
	}
}